package com.itheima.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.common.R;
import com.itheima.dto.BeanDto;
import com.itheima.entity.YddLink;
import com.itheima.service.YddLinkService;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;



@RestController
@RequestMapping("/ydd_link")
public class LinkController {

    @Autowired
    private YddLinkService linkService;

    @GetMapping("/list")
    public R<BeanDto<YddLink>> list(String type, int page, int limit) {
        //构建分页条件
        Page<YddLink> pageinfo = new Page<>(page, limit);
        //构建条件查询器
        LambdaQueryWrapper<YddLink> lqw = new LambdaQueryWrapper<>();
        lqw.like(StringUtils.isNotEmpty(type), YddLink::getType, type);

        //查询
        linkService.page(pageinfo, lqw);
        //将得到的数据装进dto中
        //当前页
        long current = pageinfo.getCurrent();

        BeanDto<YddLink> linkDto = new BeanDto();
        //存入得到的数据
        linkDto.setData(pageinfo.getRecords());
        //当前页
        linkDto.setCurrentPage(pageinfo.getCurrent());
        //记录的总数
        linkDto.setCount(pageinfo.getTotal());
        //每页大小
        linkDto.setPoageSize((long) limit);
        //总共多少页
        linkDto.setTotalPages(pageinfo.getTotal() / limit + 1);


        return R.success(linkDto);
    }

    //添加需求
    @PostMapping("/add")
    public R add(@RequestBody YddLink link) {

        linkService.save(link);
        return R.success("");
    }

    @GetMapping("/del")
    public R<String> del(int id){

        //直接根据id删除
        boolean b = linkService.removeById(id);
        if (b){
            //删除成功
            return R.success("");
        }
        //删除失败
        return null;

    }

    //查看单个标签
    @GetMapping("/rowInfo")
    public R<YddLink> getrowInfo(int id){
        YddLink byId = linkService.getById(id);
        return R.success(byId);
    }

    //修改标签
    @PostMapping("/update")
    public R update(@RequestBody YddLink tag){
        linkService.updateById(tag);
        return R.success("");

    }



    //判断字符串是否是网站 http www.com
/*
    public static boolean isHttpUrl(String urls) {
        boolean isurl = false;
        String regex = "(((https|http)?://)?([a-z0-9]+[.])|(www.))"
                + "\\w+[.|\\/]([a-z0-9]{0,})?[[.]([a-z0-9]{0,})]+((/[\\S&&[^,;\u4E00-\u9FA5]]+)+)?([.][a-z0-9]{0,}+|/?)";//设置正则表达式

        Pattern pat = Pattern.compile(regex.trim());//对比
        Matcher mat = pat.matcher(urls.trim());
        isurl = mat.matches();//判断是否匹配
        if (isurl) {
            isurl = true;
        }
        return isurl;
    }
*/


}
